package com.linewow.xhyy.nihaonew.activity.account;

import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.base.BaseActivity;
import com.linewow.xhyy.nihaonew.entity.NiHaoGeneral;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/9/20.
 */
public class ResetPwdActivity extends BaseActivity {
    @Bind(R.id.toolbar_back_title)
    TextView toolbarBackTitle;
    @Bind(R.id.toolbar_back_left)
    ImageView toolbarBackLeft;
    @Bind(R.id.reset_pwd_email_edit)
    EditText resetPwdEmailEdit;
    @Bind(R.id.reset_pwd_btn)
    RelativeLayout resetPwdBtn;
    @Bind(R.id.reset_pwd_mask)
    FrameLayout resetPwdMask;

    @Override
    public void initList() {
        super.initList();
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_reset_pwd);
        ButterKnife.bind(this);
        toolbarBackTitle.setText(getString(R.string.title_reset_pwd));
        resetPwdMask.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
               if(event.getAction()==MotionEvent.ACTION_DOWN){
                   hideSoftKeyboard();
               }
                return false;
            }
        });

    }



    @OnClick({R.id.toolbar_back_title, R.id.reset_pwd_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back_title:
                finish();
                break;
            case R.id.reset_pwd_btn:
                String email=resetPwdEmailEdit.getText().toString();
                requestInfo(email);
                break;
        }
    }

    private void requestInfo(final String email) {
        showLoading();
        NiHaoOkgo.resetInfo(email)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NiHaoGeneral<String>>() {
                    @Override
                    public void call(NiHaoGeneral<String> stringNiHaoGeneral) {
                          dismissLoading();
                        if(stringNiHaoGeneral.status){
                            showToast(getString(R.string.message_reset_send1)+email+getString(R.string.message_reset_send2));
                            finish();
                        }else{
                            showCanDialog(stringNiHaoGeneral.info);
                        }
                    }
                });
    }
}
